<?php

namespace App\Http\Controllers\Api\Wap\Dev;

use App\Http\Controllers\Common\ApiController;
use App\Models\Constant\DevConstant;
use App\Models\Dev\DevInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DevInfoController extends ApiController{


    public function __construct() {
        $this->instance = new DevInfo();
    }

    public function detail(Request $request){
        $id = $request->input('id',0);
        $record = $this->instance->newQuery()->where(compact('id'))->with(['getLog','getType'])->first();
        $record->getType->business_name = DevConstant::MAP_BUSINESS_NAME[$record->getType->business_type]??'未知';
        return view('detail',compact('record'));
    }

    public function question(Request $request){

        if(!$request->ajax()) return view('question');

        //如果是复习以前的题
        if($request->has('back')){
            $back = $request->input('back');
            $time = date('Y-m-d',strtotime("-$back day"));
            $questions = DB::table('question_back')->whereDate('created_at',$time)->get();
            return responseSuccess($questions->toArray());
        }

        //记录今天背的题
        $num = $request->input('num',3);
        $questions = DB::table('question')->get()->random($num);
        $map = [];
        $time = date('Y-m-d H:i:s');
        foreach ($questions as $question) {
            $map[] = [
                'content'=>$question->content,
                'created_at'=>$time,
                'updated_at'=>$time,
            ];
        }
        DB::table('question_back')->insert($map);
        return responseSuccess($questions->toArray());
    }
}
